package com.tushu.book.controller;

import com.tushu.book.domain.books;
import com.tushu.book.service.IBooksService;
import com.tushu.book.service.ITestDataService;
import com.tushu.book.utils.core.domain.AjaxResult;
import com.tushu.book.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/system/books")
public class bookscontroller {
    private String prefix = "system/books";
    @Autowired
    IBooksService ibooksService;
    @Autowired
    ITestDataService testDataService;

    //页面
    @RequiresPermissions("system:books:view")
    @GetMapping()
    public String books() {
        return prefix;
    }
    //查询
    @ResponseBody
    @PostMapping("/query")
    public List<books> querybook(@Validated books model){
        List<books> data = ibooksService.queryBook(model);
        return data;
    }
    //添加
    @ResponseBody
    @PostMapping("/add")
    public books addBook(@Validated books model){
        int id = ibooksService.addBook(model);
        return model;
    }
    //修改
    @ResponseBody
    @PostMapping("/updatebook")
    public boolean updatebook(@Validated books model){
        int row = ibooksService.updateBook(model);
        return row >= 0 ? true:false;
    }

    //删除
    @ResponseBody
    @GetMapping("/deletebook/{id}")
    public boolean deletebook(@PathVariable("id") int id){
        int row = ibooksService.deleteBook(id);
        return row >= 0 ? true:false;
    }

    //导出Excel
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(@Validated books model)
    {
        List<books> data = ibooksService.queryBook(model);
        ExcelUtil<books> util = new ExcelUtil<books>(books.class);
        return util.exportExcel(data, "图书数据");
    }

    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<books> util = new ExcelUtil<books>(books.class);
        List<books> userList = util.importExcel(file.getInputStream());
        for (books user: userList) {
            int id = ibooksService.addBook(user);
        }
        return AjaxResult.success("成功");
    }


    @ResponseBody
    @GetMapping("/speak/{str}")
    public String speak(@PathVariable("str") String str){
       return testDataService.speak(str);
    }

    @ResponseBody
    @RequestMapping("/getByName")
    public String getByName(String name){
        return "hi ,welcome to " + name;
    }
}
